<!-- 弹窗管理页面 -->
<template>
  <a-modal
    :visible='visible'
    title="绑定黑匣子设备"
    width="100%"
    height="100%"
    :footer="null"
    :mask-closable='false'
    :destroy-on-close='true'
    :style="{ top: '0px' }"
    @update:visible='updateVisible'
  >
    <div class="ele-body">
      <a-divider orientation="left">已关联的黑匣子设备</a-divider>
      <a-table :columns="linkDeviceColumns" :data-source="linkDeviceList" :pagination="false" bordered >
        <template #bodyCell="{ column, record }">
          <!-- table操作栏按钮 -->
          <template v-if="column.key === 'action'">
            <a-space>
              <a-popconfirm title="确定要解绑吗？" @confirm="unbind(record)">
                <a class="ele-text-success">解绑</a>
              </a-popconfirm>
            </a-space>
          </template>
        </template>
      </a-table>
      <a-divider orientation="left">关联黑匣子设备</a-divider>
      <!-- 搜索表单 -->
      <div class="block-interval">
        <a-card :bordered="false">
          <a-form layout="inline" :model="where">
            <a-row>
              <a-form-item label="所属牧场:" name="orgId">
                <a-select
                  show-search
                  v-model:value="where.ranchId"
                  placeholder="请选择所属牧场"
                  style="width: 205px;"
                  :show-arrow="false"
                  :filter-option="false"
                  :not-found-content="null"
                >
                  <a-select-option v-for="item in ranchList" :key="item.ranchId" >
                    {{ item.ranchName }}
                  </a-select-option>
                </a-select>
              </a-form-item>
              <a-form-item label="设备id:">
                <a-input style="width: 200px" v-model:value.trim="where.recordId" placeholder="请输入设备id" allow-clear />
              </a-form-item>
              <a-form-item class="ele-text-center">
                <a-space>
                  <a-button type="primary" @click="reload">查询</a-button>
                  <a-button @click="reset">重置</a-button>
                </a-space>
              </a-form-item>

            </a-row>
          </a-form>
        </a-card>
      </div>

      <!-- 表格 -->
      <div>
        <a-card :bordered="false">
          <ele-pro-table
            ref="table"
            row-key="recordId"
            :datasource="datasource"
            :columns="columns"
            :where="where"
            :scroll="{ x: 'max-content' }"
          >

            <template #bodyCell="{ column, record }">
              <!-- table操作栏按钮 -->
              <template v-if="column.key === 'action'">
                <a-space v-if="record.vehicle == null || (record.vehicle != null && record.vehicle.vehicleId != vehicleRecord.vehicleId)">
                  <a-popconfirm title="确定要绑定吗？" @confirm="bind(record)">
                    <a class="ele-text-danger">绑定</a>
                  </a-popconfirm>
                </a-space>
                <a-space v-if="record.vehicle != null && record.vehicle.vehicleId == vehicleRecord.vehicleId">
                  <a-popconfirm title="确定要绑定吗？" @confirm="unbind(record)">
                    <a class="ele-text-success">解绑</a>
                  </a-popconfirm>
                </a-space>
              </template>
            </template>
          </ele-pro-table>
        </a-card>
      </div>
    </div>

  </a-modal>
</template>

<script>

import {RanchApi} from "@/api/property/RanchApi";
import {message} from "ant-design-vue";
import {DeviceRecordApi} from "@/api/property/DeviceRecordApi";
import {VehicleApi} from "@/api/property/vechile/VehicleApi";


export default {
  name: 'VehicleManagerVehicleBinddevice',
  components: {

  },
  emits: ['update:visible'],
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  async created() {
    this.alreadyRelatelist();
    RanchApi.list({}).then(resp => {
      this.ranchList = (resp ? resp.data : null) || [];
    });
    this.vehicleRecord = this.data;
  },
  data() {
    return {
      linkDeviceList : [],
      linkDeviceColumns : [
        {
          title: '牧场',
          dataIndex: 'ranchName'
        },
        {
          title: '黑匣子唯一标识IMEI',
          dataIndex: 'clientNum'
        },
        {
          title: '关联设备ID',
          dataIndex: 'recordId'
        },
        {
          title: '关联设备号',
          dataIndex: 'recordNo'
        },
        {
          title: '操作',
          key: 'action',
          width: 140,
          align: 'center'
        }
      ],
      // 表格列配置
      columns: [
        {
          title: '牧场',
          dataIndex: 'ranchName'
        },
        {
          title: '黑匣子唯一标识IMEI',
          dataIndex: 'clientNum'
        },
        {
          title: '设备ID',
          dataIndex: 'recordId'
        },
        {
          title: '已绑定车牌号',
          dataIndex: 'recordId',
          customRender: function(row) {
            console.log(row)
            if (row.record.vehicle != null) {
              return  row.record.vehicle.license + "(车id:" + row.record.vehicle.vehicleId + ")";
            } else {
              return "未绑定车";
            }
          }
        },
        {
          title: '操作',
          key: 'action',
          width: 140,
          align: 'center'
        }
      ],
      vehicleRecord : {},
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      ranchList : [],
    };
  },
  methods: {
    alreadyRelatelist()
    {

      DeviceRecordApi.findVehicleList({"vehicleId" : this.data.vehicleId}).then(resp => {
        this.linkDeviceList = resp.data || [];
        console.log(this.linkDeviceList);
      });
    },
    /**
     * 更新编辑界面的弹框是否显示
     *
     * @param value true-显示，false-隐藏
     * @author cancan
     * @date 2022/03/05 14:48
     */
    updateVisible(value) {
      this.$emit('update:visible', value);
    },
    /**
     * 搜索按钮
     *
     * @author cancan
     * @date 2022/10/07 20:59
     */
    reload() {
      this.$refs.table.reload({ page: 1 });
      this.alreadyRelatelist();
    },

    /**
     * 重置搜索
     *
     * @author cancan
     * @date 2022/10/07 20:59
     */
    reset() {
      this.where.ranchId = '';
      this.where.recordId = '';
      this.$nextTick(() => {
        this.reload();
      });
    },

    /**
     * 删除
     *
     * @author cancan
     * @date 2022/10/07 20:59
     */
    async unbind(row) {

      let addParams = {
        recordId :  row.recordId,
        vehicleId: row.vehicle.vehicleId
      };
      const result =  await VehicleApi.unbindDeviceRecord(addParams);
      message.success(result.message);
      this.reload();
    },

    async bind(row)
    {
      let addParams = {
        recordId :  row.recordId,
        vehicleId: this.data.vehicleId
      };
      const result = await VehicleApi.bindDeviceRecord(addParams);
      message.success(result.message);
      this.reload();
    },

    /**
     * 获取表格数据
     *
     * @author fengshuonan
     * @date 2022/5/8 15:18
     */
    datasource({ page, limit, where, orders }) {
      return DeviceRecordApi.findVehiclePage({ ...where, ...orders, pageNo: page, pageSize: limit });
    }
  }
};
</script>
<style scoped>
/deep/ .ant-modal-body {
  padding: 5px;
}

</style>
